package com.cc2019.buke.home.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.cc2019.buke.R;


public class MusicPlayerService extends Service {

    private MediaPlayer player;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        if (action!=null && !"".equals(action)){
            if ("play".equals(action)){
                playMusic();
            }
        }else if ("stop".equals(action)){
            stopMusic();
        }else if ("pause".equals(action)){
            pauseMusic();
        }
        //处理用户的请求
        playMusic();
        return super.onStartCommand(intent, flags, startId);
    }

    //在服务中编写相应的功能
    public void playMusic(){
        player = MediaPlayer.create(this, R.raw.love);
        player.start();
    }
    public void pauseMusic(){
        player.pause();
    }
    public void stopMusic(){
        player.stop();
    }

    @Override
    public void onDestroy() {
        player.start();
        player.release();
        player.stop();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }
}
